package states 
{
	import assets.AssetsUI;
	import com.greensock.easing.Quint;
	import com.greensock.TweenLite;
	import org.agony2d.control.Touch;
	import org.agony2d.control.TouchManager;
	import org.agony2d.notify.AEvent;
	import org.agony2d.utils.MathUtil;
	import org.agony2d.view.AgonyUI;
	import org.agony2d.view.Fusion;
	import org.agony2d.view.LabelButton;
	import org.agony2d.view.layout.SpaceType;
	import org.agony2d.view.puppet.ImagePuppet;
	import org.agony2d.view.puppet.ShapePuppet;
	import org.agony2d.view.ScrollPane;
	import org.agony2d.view.UIState;

public class ScrollPaneUIState extends UIState
{
	
	private var numImage:int = 25
	private const gapX:int = 250
	private const gapY:int = 200;
	
	private var imageList:Array = []
	override public function enter():void
	{
		this.fusion.move(100,10)
		
		var IP:ImagePuppet
		var i:int
		var shape:ShapePuppet
		var correctionX:Number, correctionY:Number, velocityX:Number, velocityY:Number
		var F:Fusion
		var touch:Touch
		
		shape = new ShapePuppet()
		shape.graphics.beginFill(0xdd4444, 0.4)
		shape.graphics.drawRect(0, 0, 400, 400)
		this.fusion.addElement(shape, 200,80)
		
		mPane = new ScrollPane(400, 400)
		mPane.limitBottom = true
		mPane.horizDisableOffset = Infinity
		mPane.vertiDisableOffset = 4
		this.fusion.addElement(mPane, 200, 80)
		mPane.contentWidth = 2240
		mPane.contentHeight = 2100
		mPane.addEventListener(AEvent.START_DRAG, function(e:AEvent):void
		{
			if (!AgonyUI.interactive)
			{
				TweenLite.killTweensOf(mPane)
				//trace('interactive false')
			}
		})
		mPane.addEventListener(AEvent.STOP_DRAG, function(e:AEvent):void
		{
			//trace(TouchManager.getInstance().offsetX + ' | ' + TouchManager.getInstance().offsetY)
			touch = TouchManager.getInstance().primaryTouch;
			velocityX = touch.velocityX
			velocityY = touch.velocityY
			correctionX = mPane.horizCorrection
			correctionY = mPane.vertiCorrection
			if (correctionX != 0 || correctionY != 0)
			{
				AgonyUI.interactive = false
				//trace('interactive false')
				TweenLite.to(mPane, 0.7, { x:mPane.x + correctionX,
										y:mPane.y + correctionY, 
										ease:Quint.easeOut, 
										onComplete:function():void
				{
					AgonyUI.interactive = true
					//trace('interactive true')
				}})
			}
			else if(velocityX !=0 || velocityY!=0)
			{
				AgonyUI.interactive = false
				trace('interactive false')
				TweenLite.to(mPane, MathUtil.getDistance(velocityX,velocityX,velocityY,velocityY) / 4,
										{ x:mPane.x + velocityX * 30,
										y:mPane.y + velocityY * 30,
										ease:Quint.easeOut, 
				onComplete:function():void
				{
					AgonyUI.interactive = true
					trace('interactive true')
				},
				onUpdate:function():void
				{
					correctionX = mPane.horizCorrection
					correctionY = mPane.vertiCorrection
					if (correctionX != 0 || correctionY != 0)
					{
						//TweenLite.killTweensOf(mPane)
						mPane.offset(correctionX, correctionY)
						//AgonyUI.interactive = true
					}
					mPane.updateThumbs()
				}})
			}
		})
		var FA:Fusion = mPane.getHorizThumb('scroll', 400, 10)
		this.fusion.addElement(FA, 200, 480)
		var FB:Fusion = mPane.getVertiThumb('scroll', 400, 10)
		this.fusion.addElement(FB, 600, 80)
		
		while (i < numImage)
		{
			IP = new ImagePuppet(5)
			IP.embed(AssetsUI.AT_defaultImg)
			mPane.addElement(IP, (i % 5) * gapX + IP.width / 2, int(i / 5) * gapY + IP.height / 2)
			IP.addEventListener(AEvent.CLICK, function(e:AEvent):void
			{
				trace(e.target.name)
			})
			imageList[i] = IP
			i++
		}
		
		//this.fusion.filters = [new BlurFilter(10,10,3)]
		
		//KeyboardManager.getInstance().getState().addPressListener('Q', function():void
		//{
			//mPane.horizRatio = 0
			//mPane.vertiRatio = 0
		//})
		//KeyboardManager.getInstance().getState().addPressListener('E', function():void
		//{
			//mPane.horizRatio = 1
			//mPane.vertiRatio = 0
		//})
		//KeyboardManager.getInstance().getState().addPressListener('A', function():void
		//{
			//mPane.horizRatio = 0
			//mPane.vertiRatio = 1
		//})
		//KeyboardManager.getInstance().getState().addPressListener('D', function():void
		//{
			//mPane.horizRatio = 1
			//mPane.vertiRatio = 1
		//})
		//KeyboardManager.getInstance().getState().addPressListener('D', function():void
		//{
			//mPane.horizRatio = 1
			//mPane.vertiRatio = 1
		//})
		//KeyboardManager.getInstance().getState().addPressListener('L', function():void
		//{
			//mPane.locked = !mPane.locked
		//})
	}
	

	override public function exit():void
	{
		//KeyboardManager.getInstance().getState().removeAllPressListeners('Q')
		//KeyboardManager.getInstance().getState().removeAllPressListeners('E')
		//KeyboardManager.getInstance().getState().removeAllPressListeners('A')
		//KeyboardManager.getInstance().getState().removeAllPressListeners('D')
		//KeyboardManager.getInstance().getState().removeAllPressListeners('L')
		
		this.fusion.displayObject.scrollRect = null
		TweenLite.killTweensOf(mPane)
	}
	
	private var mPane:ScrollPane
}
}